<?php
/**
 * Created by PhpStorm.
 * User: lianglu
 * Date: 16/5/28
 * Time: 上午12:32
 */

namespace frontend\controllers;

use Yii;
use yii\web\Controller;

class DefaultController extends Controller
{

    public function actionIndex(){

        $echoStr = $_GET["echostr"];
        $wechat = Yii::$app->wechat;
        
        if($this->checkSignature($wechat->token)){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature($token)
    {

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }
}